var loginApp = function(timeToLoad) {
	this.busyTime = timeToLoad || 1000;
};

loginApp.prototype = function() {
	
	login = function(){
		if(_handleValidLogon()){
			sData.logOn($('#loginForm').serialize(), _handleLogOn);
		}
				/*
		$.mobile.changePage("#logon", { transition: "flip" });
        if (!index.login) {
			alert(index.login);
	    	$.mobile.changePage("#logon", { transition: "flip" });
	    	$( "#entrarButton" ).click(function() {
				if(_handleValidLogon()){
					sData.logOn($('#loginForm').serialize(), _handleLogOn);
				}
			});
	    }
	    * */
    },
	
	//Validation of login
    _handleValidLogon = function(){
		var correo = $.trim($( "#correo" ).val());
		$popup = $("#popupDialog");
		  if(correo.length == 0){
			  $popup.popup("open");
			  return false;
		  }else if(!IsEmail(correo)){
			  $popup.popup("open");
			  return false;
		  }
		  
		  var password = $.trim($( "#password" ).val());
		  if(password.length == 0){
			  $popup.popup("open");
			  return false;
		  }
		  return true;
	},
	
	// Callback login
	_handleLogOn = function (userData, success) {
		if (success) {
			index.userData = userData;
			index.login = true;
			$( "#nombreUser" ).html(userData.name);
			$.mobile.changePage('#home', { transition: 'flip' });
		}else{
			$("#popupDialog").popup("open");
		}
	};
    
	return{
		login:login
	}
}();
